﻿<?php 
// DatabaseWrapper using PDO
class Mysql {    
    private static $instance = null;
    
    function __construct($table = "avslut") {
        $this->init($table);
    }

    public function getInstance() {
        if(self::$instance === null){
            self::$instance = new Mysql();
        }
        return self::$instance;
    }
    
    private function init($table) {
        $host = "localhost:3306";
        $user = "root";
        $password = "";
        $db = "stocks";
        $engine = "mysql";
        $dns = $engine.':dbname='.$db.";host=".$host;
        
        try {
            $this->pdo = new PDO($dns , $user, $password);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            
            $this->insert_stmt = $this->pdo->prepare("INSERT INTO $table (`time`, `name`, `amount`, `price`) VALUES(?,?,?,?)");
            $this->insert_stmt2 = $this->pdo->prepare("INSERT INTO $table (`time`, `name`, `amount`, `price`, `buyer`, `seller`) VALUES(?,?,?,?,?,?)");
            $this->get_since_stmt = $this->pdo->prepare("SELECT amount,price,time FROM $table WHERE time > ? AND name=? ORDER BY time ASC ");
            $this->get_since_stmt2 = $this->pdo->prepare("SELECT amount,price,time,buyer,seller FROM $table WHERE time > ? AND name=? ORDER BY time ASC ");
            $this->get_latest_stmt = $this->pdo->prepare("SELECT time FROM `$table-latest` WHERE `name`=?");          
            $this->insert_latest_stmt = $this->pdo->prepare("INSERT INTO `$table-latest` (`time`,`name`) 
                        values(?,?) ON DUPLICATE KEY UPDATE time = ?");
            $this->getlatest_slow_stmt = $this->pdo->prepare("SELECT name, MAX(time) AS time FROM $table GROUP BY name");     
        } catch(PDOException $ex) {
            echo $ex->getMessage();
        }
    }
    
    function getLatest($stock){
        $this->get_latest_stmt->execute(array($stock));
        $arr = $this->get_latest_stmt->fetch(PDO::FETCH_ASSOC);
        if($arr["time"]=="") {
            return $this->getLatest_slow($stock); 
        }
        return $arr["time"];
    }
    
    function getLatest_slow($stock){
        $this->getlatest_slow_stmt->execute();
        $arr = $this->getlatest_slow_stmt->fetchAll(PDO::FETCH_ASSOC);
        for ($i = 0; $i < count($arr) ; $i++) {
            $row = $arr[$i];
            if( $row["name"] == $stock ) {
                    return $row["time"];
            }
        }
        return "0";
    }
    
    function setLatest($stock, $time) {
        $this->insert_latest_stmt->execute(array($time,$stock,$time));
    }

    function addEntry($stock, $time ,$num, $price ){
        $this->insert_stmt->execute(array($time,$stock,$num,$price));
        $this->setLatest($stock, $time);
    }
    
    function addEntry2($stock, $time ,$num, $price, $buyer, $seller ){
        $this->insert_stmt2->execute(array($time,$stock,$num,$price,$buyer,$seller));
    }
    
    function getSince($stock,$time){
        $this->get_since_stmt->execute(array($time, $stock));
        return $this->get_since_stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    
    function getSince2($stock,$time){
        $this->get_since_stmt2->execute(array($time, $stock));
        return $this->get_since_stmt2->fetchAll(PDO::FETCH_ASSOC);
    }
    
    function numRows($sql_query) {
        $pdo = $this->initCheck();
        $stmt = $pdo->prepare($sql_query);
        /* @var $stmt PDOStatement */
        $stmt->execute();
        $count = count($stmt->fetchAll());
        return $count;
    }
}
?>